package com.njsf.controller.front;

<<<<<<< HEAD
import com.njsf.compoent.MailUtil;
=======
import com.njsf.conponent.MailUtil;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import com.njsf.dto.ResponseJson;
import com.njsf.pojo.User;
import com.njsf.service.UserService;
import com.njsf.service.impl.UserServiceImpl;
<<<<<<< HEAD
import com.njsf.utils.ValidateCode;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
=======
import com.njsf.utils.MD5Util;
import com.njsf.utils.ValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
<<<<<<< HEAD
=======
import java.util.Map;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import java.util.UUID;

@Controller
@RequestMapping("/front")
public class UserFrontController {

    @Autowired
    private UserService userService;

<<<<<<< HEAD
    @RequestMapping("/login")
    @ResponseBody
    public ResponseJson login(String username,String password,HttpServletRequest request){
        return userService.checkLogin(username,password,request);
=======
    @Autowired
    private MailUtil mailUtil;


    @RequestMapping("/login")
    @ResponseBody
    public ResponseJson login(String username, String password, HttpServletRequest request) {
        return userService.checkLogin(username, password, request);
    }


    @RequestMapping("/chackusername")
    @ResponseBody
    public ResponseJson chackusername(String username) {
        if (username.equals("")){
            return new ResponseJson(101,"用户名不能为空");
        }
        ResponseJson responseJson = userService.selectByUsername(username);
        return responseJson;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
    }

    @RequestMapping("/validatecode")
    public void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
<<<<<<< HEAD
        ValidateCode.validateCode(request,response);
    }

    @Autowired
    private MailUtil mailUtil;
    @RequestMapping("/register")
    public void registerUser(User user,String validatecode, HttpSession session) throws Exception {
        if (!validatecode.equals((String)session.getAttribute("validatecode"))){
            return;
        }
        String activeCode = UUID.randomUUID().toString().replaceAll("-", "");
        user.setActivateCode(activeCode);
        //userService.add(user);
        System.out.println(user.getEmail());
        mailUtil.sendMailForRegisterUser(user.getEmail(),activeCode);
    }

    @RequestMapping("/activeuser")
    public void activeUser(String activeCode) throws Exception {

        System.out.println(activeCode);

    }


=======
        ValidateCode.validateCode(request, response);
    }


    @RequestMapping("/register")
    @ResponseBody
    public ResponseJson registerUser(String username, String password, String email, String name, String telephone,
                                     String validateCode, HttpSession session) throws Exception {
        String validateCode1 =  session.getAttribute("validateCode").toString();
        System.err.println(validateCode1);
        if (!validateCode.equals(validateCode1)) {
            return new ResponseJson(101, "验证码错误");
        } else {
            User user = new User();
            String activateCode = UUID.randomUUID().toString().replace("-", "");
            user.setActivateCode(activateCode);
            user.setUsername(username);
            user.setPassword(MD5Util.MD5(password));
            user.setEmail(email);
            user.setName(name);
            user.setStatus(0);
            user.setTelephone(telephone);
            mailUtil.sendMailForRegisterUser(user.getEmail(), activateCode);
            ResponseJson responseJson = userService.addUser(user);
            session.setAttribute("userEmail",user.getEmail());
            return responseJson;
        }
    }


    @RequestMapping("/activeuser")
    public String activeUser(String activeCode,HttpSession httpSession) throws Exception {
        int i = userService.activeUserByActiveCode(activeCode);
        String msg = "激活成功,请登录";
        httpSession.setAttribute("msg",msg);
        return "redirect:/front/loginview";
    }

    @RequestMapping("/registSuccess")
    public String registerSuccess() {
        return "front/registSuccess";
    }

    @RequestMapping("/logout")
    public String logOut(HttpSession httpSession) {
        httpSession.invalidate();
        return "redirect:/front/index";
    }
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
}
